package com.pro01.backend.controller;

import com.pro01.backend.dto.CommentDTO;
import com.pro01.backend.pojo.Forum2Comment;
import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.Forum2CommentService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/forum2")
public class Forum2CommentController {

    @Autowired
    private Forum2CommentService commentService;

    // 发表评论
    @PostMapping("/posts/{postId}/comments")
    public Result<Void> addComment(@PathVariable Long postId,
                                   @RequestBody Forum2Comment comment,
                                   HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);

        if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
            return Result.error("评论内容不能为空");
        }

        comment.setUserId(userId);
        comment.setPostId(postId);

        int result = commentService.addComment(comment);
        return result > 0 ? Result.success() : Result.error("评论失败");
    }

    // 查看评论列表
    @GetMapping("/posts/{postId}/comments")
    public Result<PageResult<CommentDTO>> getComments(@PathVariable Long postId,
                                                      @RequestParam(defaultValue = "1") int pageNum,
                                                      @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<CommentDTO> result = commentService.getCommentsByPostId(postId, pageNum, pageSize);
        return Result.success(result);
    }

    // 删除评论
    @DeleteMapping("/comments/{commentId}")
    public Result<Void> deleteComment(@PathVariable Long commentId, HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            String role = TokenUtil.getRole(request);

            int result = commentService.deleteComment(commentId, userId, role);
            return result > 0 ? Result.success() : Result.error("删除失败");

        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 隐藏评论
    @PutMapping("/comments/{commentId}/hide")
    public Result<Void> hideComment(@PathVariable Long commentId,
                                    HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            String role = TokenUtil.getRole(request);
            int result = commentService.updateCommentVisibility(commentId, "HIDDEN", userId, role);
            return result > 0 ? Result.success() : Result.error("隐藏失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    // 显示评论
    @PutMapping("/comments/{commentId}/unhide")
    public Result<Void> unhideComment(@PathVariable Long commentId,
                                      HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            String role = TokenUtil.getRole(request);
            int result = commentService.updateCommentVisibility(commentId, "ACTIVE", userId, role);
            return result > 0 ? Result.success() : Result.error("显示失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

}
